﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core.Lister;
using System.IO;
using COFE.Core.Utils;
using COFE.Core;
using System.Drawing;

namespace COFE.IO.FSI
{
    public class FileSystemPropertyProvider : PropertyProviderBase<FileSystemInfo>
    {
        #region Constructor
        public FileSystemPropertyProvider(string listerKey, string rootPath, FileSystemInfo entry)
            : base(entry)
        {
            registerProperties(listerKey, rootPath, entry is FileInfo);
        }
        #endregion

        #region Methods

        public override void Refresh()
        {
            base.Refresh();
            EmbeddedEntry.Refresh();
        }

        private void registerProperties(string listerKey, string rootPath, bool isFile)
        {
            base.RegisterMethod(FileSystemInfoProperties.FullName, (e, p) => { return e.FullName; });
            
            base.RegisterMethod(FileSystemInfoProperties.FullParseName, (e, p) => {
                if (!e.FullName.StartsWith(rootPath))
                    throw new Exception("");
                return COFEPath.FormatFullParseName(listerKey, e.FullName
                    .Replace(rootPath, "", StringComparison.InvariantCultureIgnoreCase)).TrimEnd('\\'); });
            base.RegisterMethod(FileSystemInfoProperties.ParentParseName, (e, p) =>
            {
                string parentPath = COFEPath.GetDirectoryName(this.GetProperty(FileSystemInfoProperties.FullParseName).ValueAsString);
                return (string.IsNullOrEmpty(parentPath)) ? null : parentPath;                    
            });

            base.RegisterMethod(FileSystemInfoProperties.Name, (e, p) => { return e.Name; });
            base.RegisterMethod(FileSystemInfoProperties.Label, (e, p) => { return e.Name; });

            base.RegisterMethod(FileSystemInfoProperties.IsExists, (e, p) => { return e.Exists; });
            base.RegisterMethod(FileSystemInfoProperties.IsFileSystem, (e, p) => { return true; });
            base.RegisterMethod(FileSystemInfoProperties.FileAttributes, (e, p) => { return (COFE.Core.FileAttributes)(int)e.Attributes; });

            base.RegisterMethod(FileSystemInfoProperties.COFEAttributes, (e, p) =>
               {
                   return (e is DirectoryInfo) ? COFEAttributes.FileSystem | COFEAttributes.SubEntriesCanChange | COFEAttributes.Browsable :
                     COFEAttributes.FileSystem;
               });

            base.RegisterMethod(FileSystemInfoProperties.CreationTimeUtc,
                (e, p) => { return e.CreationTimeUtc; },
                (e, p, v) => { e.CreationTimeUtc = (DateTime)v; });
            base.RegisterMethod(FileSystemInfoProperties.LastAccessTimeUtc,
                (e, p) => { return e.LastAccessTimeUtc; },
                (e, p, v) => { e.LastAccessTimeUtc = (DateTime)v; });
            base.RegisterMethod(FileSystemInfoProperties.LastWriteTimeUtc,
                (e, p) => { return e.LastWriteTimeUtc; },
                (e, p, v) => { e.LastWriteTimeUtc = (DateTime)v; });

            if (isFile)
            {
                base.RegisterMethod(FileInfoProperties.Length, (e, p) => { return (e as FileInfo).Length; });

                
                //base.RegisterMethod(IconProperties.LargeIcon,
                //    (e, p) =>
                //    {
                //        return Icon.ExtractAssociatedIcon(e.FullName).ToBitmap().EnsureBitmapSize(IconProperties.LargeIcon);

                //    });

            }
            else
            {

                base.RegisterMethod(DirectoryInfoProperties.DirectoryType,
                    (e, p) =>
                    {
                        if (e.FullName.Equals(rootPath)) return DirectoryTypeEnum.dtVolume;
                        return e.FullName.EndsWith(":\\") ? DirectoryTypeEnum.dtRoot : DirectoryTypeEnum.dtFolder;
                    }
                        );
                base.RegisterMethod(DirectoryInfoProperties.IsFileBasedFS, (e, p) => { return false; });
            }

        }

        #endregion

        #region Data


        #endregion

        #region Public Properties

        #endregion

    }
}
